package com.grantha.controllers;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.grantha.util.BarcodeGenerator;
import com.grantha.util.ReportGenerator;
import com.grantha.util.Util;
import com.grantha.util.Validator;

public class JasperReport extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String requestURL = request.getRequestURL().toString();
		String reportName = requestURL.split("/report/")[1].split(".pdf")[0];
		String reportPath = "/reports/" + reportName + ".jasper";
		File reportFile = new File(getServletConfig().getServletContext()
				.getRealPath(reportPath));

		if (reportFile.isFile()) {
			
			Map<String, Object> params = new HashMap<String, Object>();
			
			
			if (reportName.equals("Barcode39")) {

				String from = request.getParameter("from");
				String to = request.getParameter("to");

				if (Validator.checkBarcodeRange(from, to) && BarcodeGenerator.generateBarcode(from, to)) {

				} else {
					sendErrorMessage(response, Util.sendJSONMessage(false, "Error when generating barcode"));
					return;
				}

			} else if (reportName.equals("BookCountForAllSubjects")) {

			} else if (reportName.equals("BooksByAuthor")) {
				
				params.put("parameter1", request.getParameter("author"));

			} else if (reportName.equals("BooksBySubject")) {

				params.put("parameter1", request.getParameter("subject"));
			} else if (reportName.equals("BooksByStatus")) {

				
				params.put("parameter1", request.getParameter("status"));	

				// }else if(reportName.equals("CompleteBookList")){

				// }else if(reportName.equals("CompletePatronList")){

			} else if (reportName.equals("DueBooks")) {

			} else if (reportName.equals("IncomeFineFrom")) {

			} else if (reportName.equals("IssuedBooks")) {
				String issuedDate = request.getParameter("issuedDate");

				params.put("Parameter", issuedDate);
				params.put("Date", issuedDate);
				
					
			} else if (reportName.equals("LibraryPurchaseDetails")) {

			} else if (reportName.equals("OverdueLetter")) {

			} else if (reportName.equals("PatronHistory")) {
				
				String patNo = request.getParameter("patNo");
				params.put("parameter1", patNo);

			} else if (reportName.equals("ReservationLetter")) {

			} else if (reportName.equals("SubscriptionList")) {

			}

			

			ServletOutputStream servletOutputStream = response
					.getOutputStream();

			byte[] bytes = null;
			try {
				ReportGenerator rGen = new ReportGenerator(
						reportFile.toString());

				bytes = rGen.generate(params);

				response.setContentType("application/pdf");
				response.setContentLength(bytes.length);
				servletOutputStream.write(bytes, 0, bytes.length);
				servletOutputStream.flush();
				servletOutputStream.close();

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {

		}

	}
	private static void sendErrorMessage(HttpServletResponse response, String JsonMsg){
		response.setContentType("application/json");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(JsonMsg);
		out.close();
	}

}